package com.banzhi.shop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.adapter.OrderAffirmCartAdapter;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.entity.OrderAffirm.OrderAffirm;
import com.banzhi.shop.entity.shoppingcart.Res_Data;
import com.banzhi.shop.model.CallBack.OrderAffirmCartCallBack;
import com.banzhi.shop.model.IOrderAffirmCartModel;
import com.banzhi.shop.model.Impl.OrderAffirmCartImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.NumberUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.banzhi.shop.view.ListViewForScrollView;
import com.banzhi.shop.view.MaxLengthWatcher;

import org.json.JSONObject;

/**
 * 购物车的确认订单
 */
public class OrderAffirmCartActivity extends BaseActivity implements View.OnClickListener, OrderAffirmCartCallBack {
    /**
     * 退出
     */
    private ImageView ivback;
    /**
     * 地址
     */
    private TextView tvaddress;
    /**
     * 合计价格
     */
    private TextView tvheji;
    /**
     * 商品价格
     */
    private TextView tvgoodsprice;
    /**
     * 最后支付的价格
     */
    private TextView tvendprice;
    /**
     * 添加地址
     */
    private LinearLayout ll_add_address;
    /**
     * 留言
     */
    private EditText etliuyan;
    /**
     * 立即支付
     */
    private TextView tv_zhifu;
    /**
     * 运费
     */
    private TextView tvyunfei;
    /**
     * 商品列表
     */
    private ListView listView;

    private String liuyan;
    private String address;
    private String address_id;

    /**
     * 传过来的数据
     */
    private Res_Data data;
    private OrderAffirmCartAdapter adapter;
    private IOrderAffirmCartModel model;
    /**
     * 商品数量
     */
    private TextView tvnum;
    private TextView tvlijian;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_affirm_cart);
        Translucent.setTranslucent(this);
        Intent intent = getIntent();
        data = (Res_Data) intent.getSerializableExtra("data");
        address = data.getDefault_address().getDetail();
        address_id = data.getDefault_address().getId();
        setinits();
        setListeners();
        showViews();
        model = new OrderAffirmCartImpl();
    }

    private void showViews() {
        if (data.getDefault_address().getDetail() != null && !"".equals(data.getDefault_address().getDetail())) {
            tvaddress.setText(data.getDefault_address().getDetail());
        }
        if (ASUtils.isCanUse(data.getPriceSum())) {
            tvendprice.setText(NumberUtils.DecimalFormat(data.getPriceSum()));
        }
        if (ASUtils.isCanUse(data.getGoodsSum())) {
            tvheji.setText(NumberUtils.DecimalFormat(data.getGoodsSum()));
            tvgoodsprice.setText(NumberUtils.DecimalFormat(data.getGoodsSum()));
        }
        adapter = new OrderAffirmCartAdapter(this, data.getCart());
        listView.setAdapter(adapter);
        if (ASUtils.isCanUse(data.getDispatchprice())) {
            tvyunfei.setText(NumberUtils.DecimalFormat(data.getDispatchprice()));
        }
        if (ASUtils.isCanUse(data.getCommission())) {
            tvlijian.setText(NumberUtils.DecimalFormat(data.getCommission()));
        }
        tvnum.setText(data.getGoodsCount());
    }

    private void setinits() {
        ivback = (ImageView) findViewById(R.id.iv_back);
        tvaddress = (TextView) findViewById(R.id.tv_address);
        tvheji = (TextView) findViewById(R.id.tv_heji);
        tvgoodsprice = (TextView) findViewById(R.id.tv_goosdprice);
        tvendprice = (TextView) findViewById(R.id.tv_endprice);
        ll_add_address = (LinearLayout) findViewById(R.id.ll_add_address);
        tv_zhifu = (TextView) findViewById(R.id.tv_zhifu);
        listView = (ListViewForScrollView) findViewById(R.id.listview);
        etliuyan = (EditText) findViewById(R.id.et_liuyan);
        tvyunfei = (TextView) findViewById(R.id.tv_yunfei);
        tvnum = (TextView) findViewById(R.id.tv_num);
        etliuyan.addTextChangedListener(new MaxLengthWatcher(50, etliuyan));
        tvlijian = (TextView) findViewById(R.id.tv_lijian);
    }

    private void setListeners() {
        ivback.setOnClickListener(this);
        ll_add_address.setOnClickListener(this);
        tv_zhifu.setOnClickListener(this);
    }

    /**
     * 用于传过去的标志  表示的由选择地址进去的选择地址页面   如果是选择地址进去 选择地址后悔finish()   直接进去的啥也不干
     */
    private boolean isresult = true;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_add_address:
                Intent intent = new Intent(this, ChoiceAddressActivity.class);
                intent.putExtra("isresult", isresult);
                startActivityForResult(intent, 200);
                break;
            case R.id.tv_zhifu:
                liuyan = etliuyan.getText().toString().trim();
                if ("".equals(address)) {
                    ToastUtils.makeToastLong("请先选择收货地址");
                } else {
                    LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
                    model.zhifu(address_id, liuyan, this);
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 200:
                if (resultCode == 1) {
                    address = data.getStringExtra("address");
                    address_id = data.getStringExtra("id");
                    tvaddress.setText(address);
                    LoadingDialogUtils.showProgress(this);
                    model.QueRenOrder(address_id, OrderAffirmCartActivity.this);
                }
                break;
        }

    }

    /**
     * @param object 提交订单成功
     */
    @Override
    public void onzhifuSuccess(Object object) {
        OrderAffirm data = (OrderAffirm) object;
        Intent intent = new Intent(this, CheckstandActivity.class);
        intent.putExtra("orderid", data.getRes().getOrderInfo().getOrder_id());
        intent.putExtra("goodprice", data.getRes().getOrderInfo().getGoodsprice());
        intent.putExtra("balance", data.getRes().getBalance());
        intent.putExtra("bianma", data.getRes().getOrderInfo().getOrdersn());
        startActivity(intent);
        finish();
    }

    /**
     * @param object 提交订单失败
     */
    @Override
    public void onzhifuError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }

    @Override
    public void QueRenOrderSuccess(Object object) {
        LoadingDialogUtils.dismiss();
        Res_Data data = (Res_Data) object;

        if (ASUtils.isCanUse(data.getDispatchprice())) {
            tvyunfei.setText(NumberUtils.DecimalFormat(data.getDispatchprice()));
        }
        if (ASUtils.isCanUse(data.getPriceSum())) {
            tvendprice.setText(NumberUtils.DecimalFormat(data.getPriceSum()));
        }
    }

    @Override
    public void QueRenOrderError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }
}
